/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.comsrv.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.comsrv.entity.CnBanner;
import com.ykht.proj.comsrv.service.CnBannerService;
import com.ykht.proj.coupon.Enum.StatusEnum;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;


/**
 * 轮播图
 *
 * @author proj code generator
 * @date 2020-11-16 17:21:47
 */
@RestController
@AllArgsConstructor
@RequestMapping("/cnbanner" )
@Api(value = "cnbanner", tags = "轮播图管理")
public class CnBannerController {

    private final  CnBannerService cnBannerService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param cnBanner 轮播图
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('comsrv_cnbanner_view')" )
    public R getCnBannerPage(Page page, CnBanner cnBanner) {
		QueryWrapper<CnBanner> queryWrapper=new QueryWrapper<>();
		if(StringUtils.isNotBlank(cnBanner.getPageTypeName())){
			queryWrapper.lambda().like(CnBanner::getPageTypeName,cnBanner.getPageTypeName());
		}
		if(cnBanner.getStatus()!=null){
			queryWrapper.lambda().eq(CnBanner::getStatus,cnBanner.getStatus());
		}
		queryWrapper.lambda().orderByAsc(CnBanner::getSort);
        return R.ok(cnBannerService.page(page, queryWrapper));
    }


    /**
     * 通过id查询轮播图
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('comsrv_cnbanner_view')" )
    public R getById(@PathVariable("id" ) String id) {
        return R.ok(cnBannerService.getById(id));
    }

    /**
     * 新增轮播图
     * @param cnBanner 轮播图
     * @return R
     */
    @ApiOperation(value = "新增轮播图", notes = "新增轮播图")
    @SysLog("新增轮播图" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('comsrv_cnbanner_add')" )
    public R save(@RequestBody CnBanner cnBanner) {
		cnBanner.setCreateTime(LocalDateTime.now());
		cnBanner.setCreateUser(SecurityUtils.getUserId());
		cnBanner.setBcType("1");
    	return cnBannerService.insertBanner(cnBanner);
    }

    /**
     * 修改轮播图
     * @param cnBanner 轮播图
     * @return R
     */
    @ApiOperation(value = "修改轮播图", notes = "修改轮播图")
    @SysLog("修改轮播图" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('comsrv_cnbanner_edit')" )
    public R updateById(@RequestBody CnBanner cnBanner) {
		cnBanner.setUpdateTime(LocalDateTime.now());
		cnBanner.setUpdateUser(SecurityUtils.getUserId());
        return cnBannerService.updateBanner(cnBanner);
    }

    /**
     * 通过id删除轮播图
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除轮播图", notes = "通过id删除轮播图")
    @SysLog("通过id删除轮播图" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('comsrv_cnbanner_del')" )
    public R removeById(@PathVariable String id) {
        return R.ok(cnBannerService.removeById(id));
    }

	/**
	 * 上传
	 * @param file
	 * @return
	 */
	@ApiOperation(value = "上传文件")
	@PostMapping("uploadFile")
	public R uploadFile(@RequestParam("file") MultipartFile file) {
		return R.ok(cnBannerService.uploadFile(file));
	}

	/**
	 * 是否展示
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "是否展示")
	@GetMapping("exhibition")
	public R exhibition(Long id){
		CnBanner banner = cnBannerService.getById(id);
		UpdateWrapper<CnBanner> updateWrapper = new UpdateWrapper<>();
		if(banner.getStatus().equals(StatusEnum.normal.getValue())){
			updateWrapper.lambda().set(CnBanner::getStatus,1);
		}else {
			updateWrapper.lambda().set(CnBanner::getStatus,0);
		}
		updateWrapper.lambda().eq(CnBanner::getId,id);
		return R.ok(cnBannerService.update(updateWrapper));
	}

}
